﻿#region VSS
/* 
 * 页面名称：CollectClass
 * 页面创建人：Administrator
 * 页面创建时间：06/12/2008 20:09:47
 *********************************************
 * VSS信息
 * $Header: /MyCollection.root/MyCollection/MyCollection/CollectClass.cs 1     08-06-23 12:35a Admin $ 
 * $History: CollectClass.cs $
 * 
 * *****************  Version 1  *****************
 * User: Admin        Date: 08-06-23   Time: 12:35a
 * Created in $/MyCollection.root/MyCollection/MyCollection
 */
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;

namespace MyCollection
{
    //************************************************************************
    // 用户名				上海淘游电子商务有限公司
    // 系统名				X2HiDotNet Program
    // 子系统名                 
    // 类说明                   	
    // 作成者					James
    // 改版日					06/12/2008 20:09:47
    // 改版内容					新建
    //************************************************************************

    public class CollectClass
    {
        #region 构造函数
        public CollectClass()
        {
        }
        #endregion

        /**/
        /// <summary>
        /// 获取远程文件源代码
        /// </summary>
        /// <param name="url">远程url</param>
        /// <returns></returns>
        /// <summary>
        /// 传入URL返回网页的html代码
        /// </summary>
        /// <param name="Url">URL</param>
        /// <returns></returns>
        public string getUrltoHtml(string Url)
        {
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
                return reader.ReadToEnd();
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
           
        }

        /// <summary>
        /// 获取远程文件源代码
        /// </summary>
        /// <param name="url">Url:远程url</param>
        /// <param name="isUtf">是否是UTF8编码</param>
        /// <returns>远程文件源代码</returns>
        public static string GetRemoteHtmlCode(string url, bool isUtf)
        {
            try
            {
                string s = "";

                MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
                _xmlhttp.open("GET", url, false, null, null);
                _xmlhttp.send("");
                if (_xmlhttp.readyState == 4)
                {
                    if (isUtf)
                        s = System.Text.Encoding.UTF8.GetString((byte[])_xmlhttp.responseBody);
                    else
                        s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
                }
                return s;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// 保存远程文件
        /// </summary>
        /// <param name="remoteFile">远程文件地址</param>
        /// <param name="path">本地路径</param>
        /// <returns>返回本地路径加文件名</returns>
        public static string SaveRemoteFile(string remoteFile, string pathfile)
        {
            WebClient wc = new WebClient();
            try
            {
                wc.DownloadFile(remoteFile, pathfile);
                wc.Dispose();
                return pathfile;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
  
        /// <summary>
        /// 计算两个日期的时间间隔
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <returns></returns>
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            //string dateDiff = null;
            
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
  
            return ts.ToString();
        }

        /// <summary>
        /// 根据原价格 返回现价格
        /// </summary>
        /// <param name="price">原价格</param>
        /// <returns>计算后的价格</returns>
        public static double GetPrice(double price)
        {
            double nowPrice = 0.0;
            if (price <= 5)
                nowPrice = price + 1;
            else
                nowPrice = price + (int)(price * 0.1) + 3;
            return nowPrice;
        }


        /// <summary>
        /// 根据正则表达式来截取字段
        /// </summary>
        /// <param name="conStr">原字符串</param>
        /// <param name="startStr">正则开始</param>
        /// <param name="overStr">正则结束</param>
        /// <param name="incluL">是否包含startStr</param>
        /// <param name="incluR">是否包含overStr</param>
        /// <returns></returns>
        public static string GetBody(string conStr, string startStr, string overStr, bool incluL, bool incluR)
        {
            if (string.IsNullOrEmpty(conStr)) return "$false";
            int start, over;
            start = conStr.IndexOf(startStr);
            
            
            //去掉头部
            if (start >= 0)
            {
               conStr =  conStr.Remove(0, start);
                if (!incluL)
                    conStr = conStr.Remove(0, startStr.Length);
            }

            over = conStr.IndexOf(overStr);
            //去掉尾部
            if (over >= 0)
            {
                conStr = conStr.Remove(over + overStr.Length);

                if (!incluR)
                    conStr = conStr.Remove(over);
            }

            return conStr;
        }

        /// <summary>
        /// 将相对地址转换为绝对地址
        /// </summary>
        /// <param name="primitiveUrl">要转换的相对地址</param>
        /// <param name="consultUrl">当前网页地址</param>
        /// <returns>转换后的绝对地址</returns>
        public static string DefiniteUrl(string primitiveUrl, string consultUrl)
        {
            string reUrl = "";

            //如果前置有http:// 手动删除
            if (consultUrl.StartsWith("http://")) consultUrl = consultUrl.Remove(0, 7);

            
            consultUrl = consultUrl.Replace('\\', '/');
            //consultUrl = consultUrl.Replace("://", @":\\");
            if (consultUrl.IndexOf("/") < 0)
            {
                consultUrl = consultUrl + "/";
            }

            string [] arrayUrl = consultUrl.Split('/');

            if (primitiveUrl.StartsWith("http://"))
            {
                reUrl = primitiveUrl.Remove(0,7);
            }
            else if (primitiveUrl.StartsWith("/"))
            {
                reUrl = arrayUrl[0] + primitiveUrl;
            }
            else if (primitiveUrl.StartsWith("../"))
            {
                int pi = 0;
                while (primitiveUrl.StartsWith("../"))
                {
                    pi += 1;
                    primitiveUrl = primitiveUrl.Remove(0, 3);
                }
                for (int i = 0; i < (arrayUrl.Length-1 - pi); i++)
                {
                    if (reUrl != "")
                        reUrl = reUrl + arrayUrl[i] + "/";
                    else
                        reUrl = arrayUrl[i] + "/";
                }
                reUrl += primitiveUrl;
            }
            else
            {
                reUrl = consultUrl.Substring(0, consultUrl.LastIndexOf("/") + 1) + primitiveUrl;
            }

            return "http://" + reUrl;


        }

        /// <summary>
        /// 删除空格换行等
        /// </summary>
        /// <param name="conStr">原字符</param>
        /// <returns>新字符</returns>
        public static string GetStringByNoSpace(string conStr)
        {
            //conStr = conStr.Replace(" ", "");
            conStr = conStr.Replace("\r", "");
            conStr = conStr.Replace("\n", "");
            conStr = conStr.Replace("\t", "");
            conStr = conStr.Replace("\"", "\"\"");
            return conStr;
        }

        /// <summary>
        /// 提取链接地址,以$Array$分隔
        /// </summary>
        /// <param name="conStr">原字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="onverStr">结束字符串</param>
        /// <param name="incluL">是否包含StartStr</param>
        /// <param name="incluR">是否包含OverStr</param>
        /// <returns>链接数组</returns>
        public static string[] GetArray(string conStr, string startStr, string overStr, bool incluL, bool incluR)
        {
            if (string.IsNullOrEmpty(conStr)) return null;
            
            string regStr = @"(" + startStr + ").+?(" + overStr + ")";
            Regex regex = new Regex(regStr);
            MatchCollection mc = regex.Matches(conStr);
            string[] array = new string[mc.Count];
            int i = 0;
            foreach (Match match in mc)
            {
                array[i] = match.ToString();
                if (!incluL)
                {
                    array[i] = array[i].Replace(startStr, "");
                }

                if (!incluR)
                {
                    array[i] = array[i].Replace(overStr, "");
                }
                i += 1;
            }

            return array;
        }

        /// <summary>
        /// 过滤代码中的标签
        /// </summary>
        /// <param name="StrContent">代码</param>
        /// <param name="strRepCode">标签名</param>
        /// <returns></returns>
        public static string KillCode(string StrContent, string strRepCode)
        {
            string strRegex = "(<" + strRepCode + ">)|(<" + strRepCode + "(.+?)>)";
            Regex reg = new Regex(strRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Regex reg0 = new Regex(@"</" + strRepCode + ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            StrContent = reg.Replace(StrContent, "");
            StrContent = reg0.Replace(StrContent, "");
            return StrContent;
        }

        


    }
}
